package top.withwings.bonus.bonus.pojo.bonus.rule;

import top.withwings.bonus.bonus.pojo.Rate;

import java.util.LinkedHashMap;

/**
 * 销售总量提成
 */
public class GrossAmountBonusRule extends BonusRule{

    public static final String TYPE = "GROSS-AMOUNT";

    private int maxLevel;



    public GrossAmountBonusRule(int id, String name, LinkedHashMap<String, Double> paramMap) {
        super(id, name, paramMap);
        this.type = TYPE;
        initMaxLevel();
    }

    public Rate applyOn(double grossAmount){

        for(int  i = maxLevel ; i >0 ; i --){
            if(grossAmount >= this.paramMap.get("x"+ i)){
                return new Rate(Rate.TYPE_NORMAL, this.paramMap.get("y"+ i));
            }
        }
        return Rate.NOT_APPLIED;

    }

    private void initMaxLevel() {

        for (int i = 0; i < 100; i++) {
            if (this.paramMap.get("x" + (i + 1)) == null) {
                this.maxLevel = i;
                break;
            }
        }
    }


}